package com.peoit.honeycomb.domain;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.peoit.honeycomb.domain.util.CommonUtil;

/**
 * Created by Bing.Zeng on 2015/9/23.
 */
@Entity
@Table(name = "honeycomb_projects")
public class HoneycombProject extends BaseEntity {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	public HoneycombProject() {
		super();
		this.setId(CommonUtil.getId(4));
	}

	@Column(length = 256, nullable = true)
	private String projectName; /// 项目名称
	@Column(length = 16, nullable = true)
	private String projectCity; // 项目所在城市
	@Column(length = 256, nullable = true)
	private String address; // 项目详细地址
	@Column(length = 16, nullable = true)
	private String industryType; // 行业类型
	@Column(length = 512, nullable = true)
	private String projectIntro; // 项目介绍
	@Column(length = 128, nullable = true)
	private String moneyUse; // 资金用途
	private double totalStockMoney;// 股权总额
	private double lowestSellingPercentage;// 起售股份所占比例
	@Column(length = 128, nullable = true)
	private String stockType; // 股权类型
	private double sellStockMoney; // 发行总额
	private double perSellStockMoney;// 单股售卖金额
	private double successCondition;// 项目启动条件(投资百分比%)
	@Temporal(TemporalType.DATE)
	private Date endDate;// 结束时间
	@Column(length = 16, nullable = true)
	private String dividendType; // 分红模式
	private double dividendPercent;// 分红比例
	@Column(length = 256, nullable = true)
	private String stockholderPrivilege;// 股东特权说明
	private double projectEarnestPercent;// 项目支付保证金比例
	private double investorEarnestPercent; // 投资者保证金比例
	@Column(length = 16, nullable = true)
	private String status; /// 项目状态
	@Column(length = 256, nullable = true)
	private String marketAnalysis; // 市场分析
	@Column(length = 256, nullable = true)
	private String sourceOfIncome; // 收入来源
	@Column(length = 256, nullable = true)
	private String profitForecast; // 盈利预测
	@Column(length = 256, nullable = true)
	private String teamIntroducation; // 团队介绍
	@Column(length = 256, nullable = true)
	private String projectType; // 项目类别 动产不动产
	@Column(length = 1000, nullable = true)
	private String cancelRemarks;
	private int delayStatus; // delay次数
	@Column(length = 1000, nullable = true)
	private String delayremarks;// 延期原因

	// 发布者
	@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE }, optional = false)
	@JoinColumn(name = "publisher_id")
	private User publisher;

	// 关注该项目的用户
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name = "project_observer", joinColumns = {
			@JoinColumn(name = "project_id", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "observer_id", referencedColumnName = "id") })
	private Set<User> observers;

	// 项目的投资项
	@OneToMany(mappedBy = "project", fetch = FetchType.EAGER)
	private Set<Investment> investments;

	// 项目的各种图片
	// 投资信息
	@OneToMany(mappedBy = "project", fetch = FetchType.EAGER)
	private Set<Accessory> accessories;

	// 项目评论
	@OneToMany(mappedBy = "project", fetch = FetchType.EAGER)
	private Set<Comments> comments;

	// 财务报表
	@OneToMany(mappedBy = "project", fetch = FetchType.EAGER)
	private Set<FinanceReport> financeReport;

	/**
	 * 获取剩余可投金额
	 *
	 * @return
	 */
	public double getAvailableAmount() {
		double available = this.getSellStockMoney();
		if (investments != null) {
			for (Investment investment : investments) {
				if (investment.getStatus() != 3) {
					available -= investment.getAmount();
				}
			}
		}
		return available;
	}

	/**
	 * 获取已投金额
	 *
	 * @return
	 */
	public double getInvestedAmount() {
		double invested = 0.0;
		if (investments != null) {
			for (Investment investment : investments) {
				if (investment.getStatus() != 3) {
					invested += investment.getAmount();
				}
			}
		}
		return invested;
	}

	/**
	 * 获取是否可启动状态
	 * 
	 * @return
	 */
	public boolean getStartStatus() {
		boolean flag = false;
		if (this.getInvestedAmount() >= this.getSuccessCondition() * this.getSellStockMoney()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 获取已投人数
	 * 
	 * @return
	 */
	public Long getInvestedUserAmount() {
		long invested = 0;
		if (investments != null) {

			for (Investment investment : investments) {
				if (investment.getStatus() != 3) {
					invested++;
				}
			}
		}
		return invested;
	}

	public String getCancelRemarks() {
		return cancelRemarks;
	}

	public void setCancelRemarks(String cancelRemarks) {
		this.cancelRemarks = cancelRemarks;
	}

	public User getPublisher() {
		return publisher;
	}

	public void setPublisher(User publisher) {
		this.publisher = publisher;
	}

	public Set<User> getObservers() {
		return observers;
	}

	public void setObservers(Set<User> observers) {
		this.observers = observers;
	}

	public Set<Accessory> getAccessories() {
		return accessories;
	}

	public void setAccessories(Set<Accessory> accessories) {
		this.accessories = accessories;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public double getInvestorEarnestPercent() {
		return investorEarnestPercent;
	}

	public void setInvestorEarnestPercent(double investorEarnestPercent) {
		this.investorEarnestPercent = investorEarnestPercent;
	}

	public double getDividendPercent() {
		return dividendPercent;
	}

	public void setDividendPercent(double dividendPercent) {
		this.dividendPercent = dividendPercent;
	}

	public double getSuccessCondition() {
		return successCondition;
	}

	public void setSuccessCondition(double successCondition) {
		this.successCondition = successCondition;
	}

	public String getStockholderPrivilege() {
		return stockholderPrivilege;
	}

	public void setStockholderPrivilege(String stockholderPrivilege) {
		this.stockholderPrivilege = stockholderPrivilege;
	}

	public String getDividendType() {
		return dividendType;
	}

	public void setDividendType(String dividendType) {
		this.dividendType = dividendType;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public double getSellStockMoney() {
		return sellStockMoney;
	}

	public void setSellStockMoney(double sellStockMoney) {
		this.sellStockMoney = sellStockMoney;
	}

	public double getPerSellStockMoney() {
		return perSellStockMoney;
	}

	public void setPerSellStockMoney(double perSellStockMoney) {
		this.perSellStockMoney = perSellStockMoney;
	}

	public double getTotalStockMoney() {
		return totalStockMoney;
	}

	public void setTotalStockMoney(double totalStockMoney) {
		this.totalStockMoney = totalStockMoney;
	}

	public String getMoneyUse() {
		return moneyUse;
	}

	public void setMoneyUse(String moneyUse) {
		this.moneyUse = moneyUse;
	}

	public String getProjectIntro() {
		return projectIntro;
	}

	public void setProjectIntro(String projectIntro) {
		this.projectIntro = projectIntro;
	}

	public String getIndustryType() {
		return industryType;
	}

	public void setIndustryType(String industryType) {
		this.industryType = industryType;
	}

	public String getProjectCity() {
		return projectCity;
	}

	public void setProjectCity(String projectCity) {
		this.projectCity = projectCity;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getStockType() {
		return stockType;
	}

	public void setStockType(String stockType) {
		this.stockType = stockType;
	}

	public double getLowestSellingPercentage() {
		return lowestSellingPercentage;
	}

	public void setLowestSellingPercentage(double lowestSellingPercentage) {
		this.lowestSellingPercentage = lowestSellingPercentage;
	}

	public Set<Investment> getInvestments() {
		return investments;
	}

	public String getMarketAnalysis() {
		return marketAnalysis;
	}

	public void setMarketAnalysis(String marketAnalysis) {
		this.marketAnalysis = marketAnalysis;
	}

	public void setInvestments(Set<Investment> investments) {
		this.investments = investments;
	}

	public String getSourceOfIncome() {
		return sourceOfIncome;
	}

	public void setSourceOfIncome(String sourceOfIncome) {
		this.sourceOfIncome = sourceOfIncome;
	}

	public String getProfitForecast() {
		return profitForecast;
	}

	public void setProfitForecast(String profitForecast) {
		this.profitForecast = profitForecast;
	}

	public String getTeamIntroducation() {
		return teamIntroducation;
	}

	public void setTeamIntroducation(String teamIntroducation) {
		this.teamIntroducation = teamIntroducation;
	}

	public String getProjectType() {
		return projectType;
	}

	public void setProjectType(String projectType) {
		this.projectType = projectType;
	}

	public Set<Comments> getComments() {
		return comments;
	}

	public void setComments(Set<Comments> comments) {
		this.comments = comments;
	}

	public Set<FinanceReport> getFinanceReport() {
		return financeReport;
	}

	public void setFinanceReport(Set<FinanceReport> financeReport) {
		this.financeReport = financeReport;
	}

	public double getProjectEarnestPercent() {
		return projectEarnestPercent;
	}

	public void setProjectEarnestPercent(double projectEarnestPercent) {
		this.projectEarnestPercent = projectEarnestPercent;
	}

	public int getDelayStatus() {
		return delayStatus;
	}

	public void setDelayStatus(int delayStatus) {
		this.delayStatus = delayStatus;
	}

	public String getDelayremarks() {
		return delayremarks;
	}

	public void setDelayremarks(String delayremarks) {
		this.delayremarks = delayremarks;
	}
}
